package cn.com.homeyoda.aimate.aihealth.ui.my.family.info;

import static cn.com.homeyoda.aimate.aihealth.utils.AppConstants.DELETE_FAMILY_MEMBER;
import static cn.com.homeyoda.aimate.aihealth.utils.AppConstants.DELETE_VIDEO_SHARE;
import static cn.com.homeyoda.aimate.aihealth.utils.AppConstants.UPLOAD_VIDEO_SHARE;

import android.app.Application;

import androidx.annotation.NonNull;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.wzq.mvvmsmart.event.StateLiveData;
import com.wzq.mvvmsmart.utils.KLog;
import com.wzq.mvvmsmart.utils.RxUtils;
import com.wzq.mvvmsmart.utils.ToastUtils;

import java.util.List;

import cn.com.homeyoda.aimate.aihealth.base.BaseViewModel;
import cn.com.homeyoda.aimate.aihealth.bean.BaseResponse2;
import cn.com.homeyoda.aimate.aihealth.bean.FaceInfoBean;
import cn.com.homeyoda.aimate.aihealth.bean.IsUpdateBean;
import cn.com.homeyoda.aimate.aihealth.bean.QueryLinkBean;
import cn.com.homeyoda.aimate.aihealth.bean.UploadVideoShareBean;
import cn.com.homeyoda.aimate.aihealth.bean.VideoShareBean;
import cn.com.homeyoda.aimate.aihealth.bean.VideoShareListBean;
import cn.com.homeyoda.aimate.aihealth.data.Repository;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class MemberInfoViewModel extends BaseViewModel<Repository> {

    public StateLiveData<IsUpdateBean.DataBean> isUpdateBean;
    public StateLiveData<List<VideoShareListBean.RowsBean>> videoShareList;
    public StateLiveData<VideoShareBean.DataBean> videoShareDetail;
    public StateLiveData<VideoShareBean.DataBean> videoSharePermission;
    public StateLiveData<FaceInfoBean.DataBean> faceInfoData;
    public StateLiveData<QueryLinkBean.DataBean> queryLinkData;

    public MemberInfoViewModel(@NonNull Application application, Repository model) {
        super(application, model);
        isUpdateBean = new StateLiveData<>();

        videoShareList = new StateLiveData<>();

        videoShareDetail = new StateLiveData<>();

        videoSharePermission = new StateLiveData<>();

        faceInfoData = new StateLiveData<>();

        queryLinkData = new StateLiveData<>();

    }

    public void isUpdateMember(long id, String faceId, String userId) {
        model.isUpdateMember(String.valueOf(id), faceId, userId)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(MemberInfoViewModel.this)
                .subscribe(new Observer<IsUpdateBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        isUpdateBean.postLoading();
                    }

                    @Override
                    public void onNext(IsUpdateBean response) {
                        if (response.getCode() == 200) {
                            KLog.d(TAG, "onNext response = " + response);
                            isUpdateBean.postValueAndSuccess(response.getData());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        isUpdateBean.postError();
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }


    public void deleteFamilyMember(String faceId) {
        model.deleteFamilyMember(model.getFamilyId(), faceId)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(MemberInfoViewModel.this)
                .subscribe(new Observer<BaseResponse2>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(BaseResponse2 response) {
                        if (response.getCode() == 200) {
                            KLog.d(TAG, "onNext response = " + response);
                            LiveEventBus.get(DELETE_FAMILY_MEMBER).post(true);
                        } else {
                            ToastUtils.showShort(response.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }

    public void getVideoShareList(long id) {

        model.getVideoShareList(id, 1, 10)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(MemberInfoViewModel.this)
                .subscribe(new Observer<VideoShareListBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        videoShareList.postLoading();
                    }

                    @Override
                    public void onNext(VideoShareListBean response) {
                        if (response.getCode() == 200) {
                            KLog.d(TAG, "onNext response = " + response);
                            videoShareList.postValueAndSuccess(response.getRows());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        videoShareList.postError();
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }

    public void getVideoShare(long id, boolean isPermission) {
        model.getVideoShare(id)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(MemberInfoViewModel.this)
                .subscribe(new Observer<VideoShareBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        if (isPermission) {
                            videoSharePermission.postLoading();
                        } else {
                            videoShareDetail.postLoading();
                        }
                    }

                    @Override
                    public void onNext(VideoShareBean response) {
                        if (response.getCode() == 200) {
                            KLog.d(TAG, "onNext response = " + response);
                            if (isPermission) {
                                videoSharePermission.postValueAndSuccess(response.getData());
                            } else {
                                videoShareDetail.postValueAndSuccess(response.getData());
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        if (isPermission) {
                            videoSharePermission.postError();
                        } else {
                            videoShareDetail.postError();
                        }
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }

    public void addVideoShare(UploadVideoShareBean bean) {
        model.uploadVideoShare(bean)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(MemberInfoViewModel.this)
                .subscribe(new Observer<BaseResponse2>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(BaseResponse2 response) {
                        if (response.getCode() == 200) {
                            KLog.d(TAG, "onNext response = " + response);
                            LiveEventBus.get(UPLOAD_VIDEO_SHARE).post(true);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }

    public void updateVideoShare(UploadVideoShareBean bean) {
        model.updateVideoShare(bean)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(MemberInfoViewModel.this)
                .subscribe(new Observer<BaseResponse2>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(BaseResponse2 response) {
                        if (response.getCode() == 200) {
                            KLog.d(TAG, "onNext response = " + response);
                            LiveEventBus.get(UPLOAD_VIDEO_SHARE).post(true);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }

    public void deleteVideoShare(long id) {
        model.deleteVideoShare(id)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(MemberInfoViewModel.this)
                .subscribe(new Observer<BaseResponse2>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(BaseResponse2 response) {
                        if (response.getCode() == 200) {
                            KLog.d(TAG, "onNext response = " + response);
                            LiveEventBus.get(DELETE_VIDEO_SHARE).post(true);
                        } else {
                            ToastUtils.showShort(response.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }

    public void faceInfo(String faceId){

        model.queryFaceInfo(faceId, model.getFamilyId())
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(MemberInfoViewModel.this)
                .subscribe(new Observer<FaceInfoBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        faceInfoData.postLoading();
                    }

                    @Override
                    public void onNext(FaceInfoBean faceInfoBean) {
                        faceInfoData.postValueAndSuccess(faceInfoBean.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        faceInfoData.postError();
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }

    public void queryLink(String link){

        model.queryLink(link)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(MemberInfoViewModel.this)
                .subscribe(new Observer<QueryLinkBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        queryLinkData.postLoading();
                    }

                    @Override
                    public void onNext(QueryLinkBean response) {
                        if (response.getCode() == 200) {
                            queryLinkData.postValueAndSuccess(response.getData());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        queryLinkData.postError();
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }

}
